from ast import alias
from sqlalchemy import select, update
from app.api.admin.model.client.client import ClientModel
from app.api.admin.schema.client.client import ClientChangeStatusSchema, ClientListInSchema, ClientOutSchema
from app.dependencies.database import db


class ClientService:
    @classmethod
    async def instance(cls):
        return cls()

    async def list(self, list_in: ClientListInSchema):
        records = await db.fetch_all(select(ClientModel).offset(list_in.page_size*(list_in.page-1)).limit(list_in.page_size))
        return [ClientOutSchema.model_validate(record, from_attributes=True).model_dump(by_alias=True) for record in records]

    async def change_status(self, client_in: ClientChangeStatusSchema):
        await db.execute(update(ClientModel).where(ClientModel.id == client_in.client_id).values(status=client_in.status))
